package com.parking.stall.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.parking.common.core.model.StateData;
import com.parking.stall.dto.TimeDto;
import com.parking.stall.form.RentTimeForm;
import com.parking.stall.service.call.StringDispose;

import java.util.*;

public class Test {
    public static void main(String[] args) {
        String timePlan = "08,09,10,11,14,15,16,17,22,23";
        System.out.println(fromTimePlanToHumanTimePlan(timePlan));
    }

    public static String fromTimePlanToHumanTimePlan(String timePlan){
        String humanTimePlan = "";

        String[] timePlans = timePlan.split(",");
        int startIndex = 0;
        int endIndex = 0;
        for(int i=0;i<timePlans.length;i++){
            if(i+1==timePlans.length) {
                if(startIndex!=0) humanTimePlan += ",";
                humanTimePlan += timePlans[startIndex] + "点-" + next(timePlans[endIndex]) + "点";
            }else {
                if(next(timePlans[i]).equals(timePlans[i+1])){
                    endIndex++;
                }else{
                    if(startIndex!=0) humanTimePlan += ",";
                    humanTimePlan += timePlans[startIndex] + "点-" + next(timePlans[endIndex]) + "点";
                    endIndex++;
                    startIndex = endIndex;
                }
            }
        }

        return humanTimePlan;
    }

    public static String next(String time){
        // time 00-23 --> 01-24
        return fromIntegerToString(fromStringToInteger(time)+1);
    }

    public static Integer fromStringToInteger(String time){
        // 00-24
        if(time.charAt(0)=='0') return Integer.valueOf(""+time.charAt(1));
        return Integer.valueOf(time);
    }

    public static String fromIntegerToString(Integer timeInt){
        // 00-24
        if(timeInt<10) return "0"+timeInt;
        return ""+timeInt;
    }




//    //时间段判断设置
//    String rentTime = stallRentTime.getRentTime();
//    JSONArray jsonArray = JSONUtil.parseArray(rentTime);
//    List<JSONObject> jsonObjectList = jsonArray.toList(JSONObject.class);
//    ArrayList<RentTimeForm> rentTimeForms = new ArrayList<>();
//
//    for (JSONObject jsonObject : jsonObjectList) {
//        int startTime = jsonObject.getInt("startTime");
//        int endTime = jsonObject.getInt("endTime");
//        RentTimeForm timeRange = new RentTimeForm(startTime, endTime);
//        rentTimeForms.add(timeRange);
//    }
//    System.out.println(rentTimeForms);
//    String src="";
//   for (RentTimeForm rent : rentTimeForms) {
//        if (rent!=null) {
 //          if (src.equals(""))  src = rent.getStartTime()+","+rent.getEndTime();
//           else src =src+","+rent.getStartTime()+","+rent.getEndTime();
//        }
//    }
//    List<Integer> indexList = StringDispose.getRepetitionIndex(src);
//    String numStringSort = StringDispose.numStringSort(src);
//    String numStringDeduplication = StringDispose.getNumStringDeduplication(numStringSort, indexList);
//    List<String> numberList = Arrays.asList(numStringDeduplication.split(","));
//
//    Set<TimeDto> timeDtoSet = new HashSet<>();
//    for (int i = 0; i < numberList.size(); i++) {
//        TimeDto timeDto = new TimeDto();
//        if(2*i+1<numberList.size()){
//            String s1 = numberList.get(2*i);
//            String s2 = numberList.get(2*i+1);
//            timeDto.setStartTime(Integer.parseInt(s1));
//            timeDto.setEndTime(Integer.parseInt(s2));
//            timeDtoSet.add(timeDto);
//        }
//    }
//                dto.setIsPlan(StateData.STALL_IS_PLAN);
//                dto.setPlanTimes(timeDtoSet);




















}
